/* Copyright (c) [2024] [ylc]. Licensed under Apache-2.0 License. */

#include "dis_7seg.h"

DIS_SEG_T DIS_SEG={0};

u8 AsciiBuf[128]={
	0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,	//0 1 2 3 4 5 6 7 8 9 
	0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x00,0x00,0x00,	//A B C D E F 
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x3F,0x06,	//          -     0 1 
	0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x00,	//2 3 4 5 6 7 8 9     
	0x00,0x00,0x00,0x00,0x00,0x77,0x7c,0x39,0x5e,0x79,	//          A B C D E 
	0x71,0x3d,0x76,0x0f,0x0e,0x75,0x38,0x37,0x54,0x5c,	//F G H I J K L M N O
	0x73,0x67,0x50,0x49,0x78,0x3e,0x1c,0x7e,0x64,0x6e,	//P Q R S T U V W X Y 
	0x59,0x00,0x00,0x00,0x00,0x08,0x00,0x77,0x7c,0x39,	//Z         _   A B C 
	0x5e,0x79,0x71,0x3d,0x76,0x0f,0x0e,0x75,0x38,0x37, 	//D E F G H I J K L M
	0x54,0x5c,0x73,0x67,0x50,0x49,0x78,0x3e,0x1c,0x7e,	//N O P Q R S T U V W 
	0x64,0x6e,0x59,0x00,0x06,0x00,0x00,0x00				//X Y Z   |       
};

//数字 负号
void dis_num(u16 num,u8 unit)
{
	u8 tmp;
	u8 i;

	DIS_SEG.buf[0]=0;
	DIS_SEG.buf[1]=0;
	DIS_SEG.buf[2]=0;
	for(i=0;i<3;i++){
		tmp=num%10;
		DIS_SEG.buf[2-i]=AsciiBuf[tmp];
		num/=10;
		if(num==0){
			if(unit){
				if(2-i-1>=0)
					DIS_SEG.buf[2-i-1]=AsciiBuf['-'];
			}
			break;//高位0不显示
		}
	}
}

void dis_str(u8 *str)
{
	u8 i;

	DIS_SEG.buf[0]=0;
	DIS_SEG.buf[1]=0;
	DIS_SEG.buf[2]=0;
	for(i=0;i<3;i++){
		if(str[i]==0){
			break;
		}
		DIS_SEG.buf[i]=AsciiBuf[str[i]];
	}
}

/**************************  测试代码  **************************/


void dis_led_1ms(void)
{
	static u8 led_com=0;
	
	COM1=1;
	COM2=1;
	COM3=1;
	
	SEG1=DIS_SEG.b[led_com].A;
	SEG2=DIS_SEG.b[led_com].B;
	SEG3=DIS_SEG.b[led_com].C;
	SEG4=DIS_SEG.b[led_com].D;
	SEG5=DIS_SEG.b[led_com].E;
	SEG6=DIS_SEG.b[led_com].F;
	SEG7=DIS_SEG.b[led_com].G;
	SEG8=DIS_SEG.b[led_com].H;
	
	switch(led_com){
		case 0:COM1=0;break;
		case 1:COM2=0;break;
		case 2:COM3=0;break;
		default:break;
	}
	led_com++;
	if(led_com>=3)
		led_com=0;
}


void dis_test(void)
{
	u16 i;

	dis_str(" |_");		delay_ms(1000);
	dis_str("---");		delay_ms(1000);
	dis_str("error");	delay_ms(1000);
	DIS_SEG.buf[0]=AsciiBuf['R'];
	DIS_SEG.buf[1]=AsciiBuf['E'];
	DIS_SEG.buf[2]=AsciiBuf['G'];
	delay_ms(1000);
	
	for(i=0x00;i<=0x0f;i++){
		delay_ms(500);
		DIS_SEG.buf[0]=AsciiBuf[i];
		DIS_SEG.buf[1]=AsciiBuf[i];
		DIS_SEG.buf[2]=AsciiBuf[i];
	}
	delay_ms(1000);
	for(i=0;i<99;i++){
		delay_ms(100);
		dis_num(i,1);
	}
}


